package model;

import java.util.ArrayList;

public class Round {
	private boolean isStarted;
	private boolean showDown;
	private boolean hasEnded;
	private ArrayList<Player> playingList = new ArrayList<Player>();
	private Pot pot = null;
	private int lastBet = 0;
	private Player lastBetOwner = null;
	private ArrayList<Card> cardList = new ArrayList<Card>();
	
	public Round(ArrayList<Player> playingList){
		this.playingList = playingList;
		this.isStarted = true;
		this.showDown = false;
		this.hasEnded = false;
	}
	
	public int collectBlinds(ArrayList<Player> playingList, int bigBlind){
		int pot = 0;
		int currentBet = 0;
		for(Player player : playingList){
			currentBet = player.payBlind(bigBlind);
			pot = pot + currentBet;
			if(currentBet>=this.lastBet)	//storing the lastBet
				lastBet = currentBet;
			if(player.isBigBlind())
				this.lastBetOwner = player;
		}
		return pot;
	}
	
	public void determineNextToAct(Dealer dealer){
		int lastToAct = 0;
		int nextToAct = 0;
		for(int i=0; i<playingList.size(); i++){
			if(playingList.get(i).isLastToAct())
				lastToAct = i;
		}
		
		nextToAct = lastToAct+1;
		if(nextToAct==playingList.size())
			nextToAct = 0;
		
		for(Player player : playingList)
			player.setLastToAct(false);

		while(playingList.get(nextToAct).hasFolded()){
			nextToAct++;
			if(nextToAct==playingList.size())
				nextToAct = 0;
		}
		
		playingList.get(nextToAct).setTurn(true);
		determineWhatToAct(playingList.get(nextToAct), dealer);
	}
	
	public void determineWhatToAct(Player player, Dealer dealer){
		

		if(this.cardList.size()==5 && this.lastBetOwner==player) {
			this.showDown = true;
			player.setCanFold(false);
			player.setCanRaise(false);
			player.setCanCall(false);
			player.setCanCheck(false);
		}
		
		else if(player.getBet()!=this.lastBet){
			player.setCanFold(true);
			player.setCanRaise(true);
			player.setCanCall(true);
			player.setCanCheck(false);
		}
		else{	
			if(this.cardList.size()==0 && this.lastBetOwner==player){
				dealer.draw();
				this.setCardList(dealer.dealFlop());
			}
			else if(this.cardList.size()==3 && this.lastBetOwner==player){
				dealer.draw();
				this.getCardList().add(dealer.draw());
			}
			else if(this.cardList.size()==4 && this.lastBetOwner==player){
				dealer.draw();
				this.getCardList().add(dealer.draw());
			}
			player.setCanFold(false);
			player.setCanCall(false);
			player.setCanCheck(true);
			player.setCanRaise(true);
		}
	}
	
	public void setStarted(boolean isStarted) {
		this.isStarted = isStarted;
	}

	public boolean isStarted() {
		return isStarted;
	}

	public boolean isShowDown() {
		return showDown;
	}

	public void setShowDown(boolean showDown) {
		this.showDown = showDown;
	}

	public boolean isHasEnded() {
		return hasEnded;
	}

	public void setHasEnded(boolean hasEnded) {
		this.hasEnded = hasEnded;
	}

	public void setPot(Pot pot) {
		this.pot = pot;
	}

	public Pot getPot() {
		return pot;
	}

	public void setPlayingList(ArrayList<Player> playingList) {
		this.playingList = playingList;
	}

	public ArrayList<Player> getPlayingList() {
		return playingList;
	}

	public void setLastBet(int lastBet) {
		this.lastBet = lastBet;
	}

	public int getLastBet() {
		return lastBet;
	}

	public void setCardList(ArrayList<Card> cardList) {
		this.cardList = cardList;
	}

	public ArrayList<Card> getCardList() {
		return cardList;
	}

	public void setLastBetOwner(Player lastBetOwner) {
		this.lastBetOwner = lastBetOwner;
	}

	public Player getLastBetOwner() {
		return lastBetOwner;
	}

}
